import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class CustomAppBar extends StatelessWidget implements PreferredSizeWidget {
  final String? title;
  final String? actionName;
  final IconData? actionIcon;
  final VoidCallback? action;

  const CustomAppBar({super.key, this.title, this.actionName, this.action, this.actionIcon});

  @override
  Size get preferredSize => const Size.fromHeight(kToolbarHeight);

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final colorScheme = theme.colorScheme;
    final isDarkMode = theme.brightness == Brightness.dark;

    // AppBar背景色
    final appBarColor = colorScheme.surface;

    // 文本和图标颜色
    final foregroundColor = colorScheme.onSurface;

    // 按钮背景色
    final buttonBgColor = colorScheme.primary;

    // 按钮文本颜色
    final buttonTextColor = colorScheme.surface;

    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: SystemUiOverlayStyle(
        statusBarColor: appBarColor,
        statusBarIconBrightness: isDarkMode ? Brightness.light : Brightness.dark,
        systemNavigationBarColor: colorScheme.primary,
        systemNavigationBarIconBrightness: isDarkMode ? Brightness.light : Brightness.dark,
      ),
      child: Material(
        color: appBarColor,
        elevation: 0,
        child: SafeArea(
          bottom: false,
          child: SizedBox(
            height: preferredSize.height,
            child: Padding(
              padding: const EdgeInsets.symmetric(horizontal: 16),
              child: Row(
                children: [
                  // 返回按钮 + 标题
                  SizedBox(
                    height: 44,
                    child: InkWell(
                      borderRadius: BorderRadius.circular(22),
                      onTap: () => Navigator.maybePop(context),
                      child: Padding(
                        padding: const EdgeInsets.symmetric(horizontal: 8),
                        child: Row(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            Icon(Icons.arrow_back_ios, size: 16, color: foregroundColor),
                            if (title != null) const SizedBox(width: 8),
                            if (title != null)
                              Text(
                                title!,
                                style: theme.textTheme.titleMedium?.copyWith(
                                  fontWeight: FontWeight.bold,
                                  color: foregroundColor,
                                ),
                              ),
                          ],
                        ),
                      ),
                    ),
                  ),
                  const Spacer(),
                  // 右侧动作按钮
                  if (action != null)
                    SizedBox(
                      height: 36,
                      child: ElevatedButton(
                        onPressed: action,
                        style: ElevatedButton.styleFrom(
                          backgroundColor: buttonBgColor,
                          foregroundColor: buttonTextColor,
                          elevation: 0,
                          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(5)),
                          padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
                          textStyle: theme.textTheme.labelLarge,
                        ),
                        child: Row(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            if (actionIcon != null) Icon(actionIcon, size: 18),
                            if (actionIcon != null && (actionName?.isNotEmpty ?? false)) const SizedBox(width: 6),
                            if (actionName?.isNotEmpty ?? false) Text(actionName!),
                          ],
                        ),
                      ),
                    ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}
